home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_12 / allison / tstr.cpp < prev   
Encoding:
C/C++ Source or Header  |  1994-10-04  |  864 b   |  35 lines

  1. LISTING 6 - Illustrates the string class
  2.  
  3. // tstr.cpp:     Test the C++ string class
  4. #include <iostream.h>
  5. #include <stddef.h>
  6. #include <cstring.h>    // Borland's name for <string>
  7.  
  8. main()
  9. {
  10.     string s1("Mary had a little lamb"),
  11.            s2 = "Old McDonald had a farm";
  12.  
  13.     // Test some operators
  14.     string s3 = s1 + ", but " + s2;
  15.     cout << s3 << endl;
  16.     s3.resize(s1.length());
  17.     cout << "s1 == s3? " << (s1 == s3 ? "yes" : "no") << endl;
  18.  
  19.     // Search and replace
  20.     size_t pos = s2.find("farm");
  21.     s2.insert(pos,"little ");
  22.     cout << "s2 == " << s2 << endl;
  23.  
  24.     // Subscripting
  25.     s1[s1.length()-1] = 'p';
  26.     cout << "s1 == " << s1 << endl;
  27.     return 0 ;
  28. }
  29.  
  30. // Output:
  31. Mary had a little lamb, but Old McDonald had a farm
  32. s1 == s3? yes
  33. s2 == Old McDonald had a little farm
  34. s1 == Mary had a little lamp
  35.